package com.sh.data.engine.domain.datadev.offline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.data.engine.domain.base.model.UserContext;
import com.sh.data.engine.domain.datadev.offline.service.DatadevOperationHistoryService;
import com.sh.data.engine.domain.workspace.manager.model.domain.UserDomain;
import com.sh.data.engine.domain.workspace.manager.service.UserService;
import com.sh.data.engine.repository.dao.datadev.DatadevOperationHistoryMapper;
import com.sh.data.engine.repository.dao.datadev.entity.DataDevOperationHistoryEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DatadevOperationHistoryServiceImpl
    extends ServiceImpl<DatadevOperationHistoryMapper, DataDevOperationHistoryEntity>
    implements DatadevOperationHistoryService {

    @Autowired
    private UserService userService;

    @Override
    public void insertHistory(Long nodeId, Long projectId, String remark) {
        final UserDomain userInfo = UserContext.getUserInfo();
        final String username = userInfo.getUserName();
        final String id = userInfo.getId();

        DataDevOperationHistoryEntity entity = new DataDevOperationHistoryEntity();
        entity.setProjectId(projectId);
        entity.setNodeId(nodeId);
        entity.setRemark(remark);
        entity.setUsername(username);
        entity.setCreatorId(id);
        this.save(entity);
    }

    @Override
    public void insertHistory2(Long nodeId, String userId, Long projectId, String remark) {
        UserDomain userDomain = userService.getUserById(userId);
        DataDevOperationHistoryEntity entity = new DataDevOperationHistoryEntity();
        entity.setProjectId(projectId);
        entity.setNodeId(nodeId);
        entity.setRemark(remark);
        if (userDomain != null) {
            entity.setUsername(userDomain.getUserName());
        }
        entity.setCreatorId(userId);
        this.save(entity);
    }

    @Override
    public Page<DataDevOperationHistoryEntity> pageQuery(
        List<Long> nodeIdList, String content, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<DataDevOperationHistoryEntity> lambdaQueryWrapper =
            new LambdaQueryWrapper<DataDevOperationHistoryEntity>()
                .in(DataDevOperationHistoryEntity::getNodeId, nodeIdList);
        if (StringUtils.isNotBlank(content)) {
            lambdaQueryWrapper.and(
                wrapper ->
                    wrapper
                        .like(DataDevOperationHistoryEntity::getUsername, content)
                        .or()
                        .like(DataDevOperationHistoryEntity::getRemark, content));
        }
        Page<DataDevOperationHistoryEntity> page =
            page(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        return page;
    }
}
